<?php
/**
 * Created by PhpStorm.
 * User: dell
 * Date: 2017/6/3
 * Time: 10:57
 */

namespace Weixin\Model\User;


use Think\Model\RelationModel;

class UserModel extends RelationModel
{
    /*protected $_link = array(
        'User_weixin' => array(
            'mapping_type' => self::HAS_ONE,
            'class_name' => 'user_weixin',
            // 'condition'=>'weixin_id=25',
            //'foreign_key'=>'user_id',
            //'mapping_name' =>'userweixin',
            // 'mapping_fields'=>'weixin_id',
            'as_fields' => 'weixin_id',
        ),
    );*/

    protected $_link = array(
        'user_weixin' => array(
            'mapping_type' => self::HAS_ONE,
            'as_fields' => 'weixin_id'
        )
    );


    public function findUser($id)
    {
        return $this->where(array('id' => $id))->relation(true)->find();
    }

    public function addUser($nickname, $last_time)
    {
        return $this->add(array('nickname' => $nickname, 'last_time' => $last_time));
    }

    public function cha()
    {
        // return $this->db(0,"mysql://root:root@localhost/weixin2#utf8")->select();
        // return $this->db(2,"mysql://root:root@localhost/weixin2#utf8")->select();
        return $this->select();
    }

    public function cha2()
    {
        return $this->join('user_weixin uw on uw.user_id=user.id')->select();
        //return $this->alias('u')->join('user_weixin uw on uw.user_id=u.id')->select();
        //return $this->table(array('user'=>'u','user_weixin'=>'uw'))->where('u.id=uw.user_id')->select();

        //return $this->relation(true)->select();
    }

    public function test2()
    {
        //return $this->select();
        //return $this->field('id,nickname')->select();
        // return $this->field(array('id', 'update_time'))->select();
        // return $this->field(array('id', 'update_time'=>'time'))->select();
        //return $this->field(nickname)->select();
        // return $this->field('nickname as name')->select();
        //  return $this->field('SUM(nickname)')->select();
        //return $this->field('COUNT(id)')->select();
        //return $this->field('SUM(nickname) as sum')->select();
        // return $this->field(array('id','concat(nickname,'-',id)'=>'truename'))->select();
        //return $this->field(array('id','SUM(nickname)'=>'sum'))->select();
        //return $this->field(true)->select();
        // return $this->field('nickname', true)->select();
        //return $this->field('id,nickname', true)->select();
        // return $this->field(array('id', 'update_time'), true)->select();
        $data['nickname'] = '4';
        $data['update_time'] = '44';
        $data['id'] = 32;
        return $this->field('nickname')->save($data);

        //return $this->field('id,title')->table('vote_item.onethink_addons')->select();
        //return $this->field('id,title')->order('id')->table('vote_item.onethink_addons')->select();
        // return $this->field('id,title')->order('id desc')->table('vote_item.onethink_addons')->select();
        //return $this->field('id,title,create_time')->order('create_time desc,id')->table('vote_item.onethink_addons')->select();
        //return $this->field('id,title,create_time')->order('create_time desc,id desc')->table('vote_item.onethink_addons')->select();

    }

    public function test3()
    {
        // return $this->table('meiyile.onethink_document')->field('title')->limit(0, 4)->select();
        //return $this->table('meiyile.onethink_document')->field('title')->limit(4, 4)->select();
        //return $this->table('meiyile.onethink_document')->field('title')->page(1, 2)->select();
        //return $this->table('meiyile.onethink_document')->field('title')->page(2, 2)->select();
        // return $this->table('meiyile.onethink_document')->field('title')->page(3, 2)->select();
        // return $this->table('meiyile.onethink_document')->field('title')->limit(2)->page(3)->select();
        //return $this->select();
        //return $this->group('course')->select();
        return $this->group('nickname')->select();
    }

    public function test4()
    {
        //return $this->group('course')->select();
        // return $this->group('course')->having("nickname='李四'")->select();
        // return $this->group('course')->where("nickname='李四'")->select();
        return $this->table('user_weixin')->join('user on user_weixin.user_id=user.id')->join('weixin on user_weixin.weixin_id=weixin.id')->select();
    }

    public function test6()
    {
        // return $this->table('user_weixin')->join('__USER__ on user_weixin.user_id=__USER__.id')->join('__WEIXIN__ on user_weixin.weixin_id=__WEIXIN__.id')->select();
        //  return $this->table('user')->join('RIGHT JOIN user_weixin on user.id=user_weixin.user_id')->select();
        //  return $this->table('user')->join('user_weixin on user.id=user_weixin.user_id', 'RIGHT')->select();
        // return $this->table('user_weixin')->join(array('user on user.id=user_weixin.user_id', 'weixin on weixin.id=user_weixin.weixin_id'))->select();
        return $this->table('user_weixin')->join(array('LEFT join user on user.id=user_weixin.user_id', 'RIGHT join weixin on weixin.id=user_weixin.weixin_id'))->select();
    }

    public function test7()
    {
        // return $this->field('id')->table('user_weixin')->union('select id from user')->union('select id from weixin')->select();
        // return $this->field('id')->table('user_weixin')->union(array('field' => 'id', 'table' => 'user'))->union(array('field' => 'id', 'table' => 'weixin'))->select();
        // return $this->field('id')->table('user_weixin')->union('select id from user', true)->union('select id from weixin', true)->select();

        // return $this->field('id')->union('select id from user_weixin')->union('select id from user')->union('select id from weixin')->select();

        return $this->field('id')->table('user_weixin')->union(array('select id from user', 'select id from weixin'))->select();
        // return $this->field('id')->table('user_weixin')->union(array('select id from user', 'select id from weixin', true))->select();
    }

    public function test8()
    {
        return $this->field('vote_time')->distinct(true)->select();
        // return $this->lock(true)->select();

        //  return $this->cache(true, 60)->select();
        // return $this->cache('data1', 15)->select();
        //  return $this->comment('查询user表所有数据')->select();
        //  return $this->relation(true)->select();
        // return $this->fetchSql(true)->select();
        //return $this->token(false)->create();
        //$data = create();
        //dump($data);
    }

    public function test9($data)
    {
        $this->create($data);
        $this->vote_time = 999;
        return $this->add();
    }

    protected $_scope = array(
        'normal' => array(
            'where' => array('vote_time' => 123)
        ),
        'lastest' => array(
            /*   'where' => array('vote_time' => 222),*/
            'limit' => 2,
            'order' => 'id desc'
        )
    );

    /*protected $_scope = array(
        'default' => array(
            'where' => array('vote_time' => 123),
            'order' => 'id desc'
        )
    );*/

    public function test11($data)
    {
        return $this->strict(false)->add($data);
        //  return $this->scope('lastest')->select();
        // return $this->scope('new,normal')->scope('lastest')->select();
        // return $this->scope('normal,lastest')->select();
        //  return $this->scope()->select();
        // return $this->scope('normal,lastest', array('limit' => 1))->select();
        // return $this->scope(array('where' => 'vote_time=123', 'order' => 'id desc', 'limit' => 1))->select();
        //  return $this->scope('normal,lastest')->limit(1)->select();
        // return $this->limit(1)->scope('normal,lastest')->select();
        // return $this->scope('normal', array('limit' => 1))->select();
        // return $this->normal(array('limit' => 1))->select();
    }

    public function test12($data)
    {
        return $this->create($data);
    }

    public function test13($data)
    {
        return $this->create($data);
    }

    protected $insertFields = 'vote_time';

    public function test14($data)
    {
        $this->create($data);
        return $this->add();
    }
}